from Data import Data
from MetaData import MetaData,MetaDataFactory
import activist.Time

class Article(Data):
    def __init__(self,expectedid=None):
        super(Article,self).__init__(expectedid)
        self.command("create")
        self.command("edit")
        self.command("editmeta")

    command_create_VERSION = 1
    def command_create(self,title,content,creator=None,up=None,next=None,prev=None):
        self._createcalled = True
        self._title = title
        
        if creator:
            MetaDataFactory(self,MetaData.Creator,creator)

        if not content:
            raise "Need content!"
        MetaDataFactory(self,MetaData.InformationOf,content)

        self._up = up
        self._next = next
        self._prev = prev

    def title(self):
        return self._title

    command_edit_VERSION = 0
    def command_edit(self,text):
        return MetaDataFactory(self,MetaData.InformationOf,text)

    command_editmeta_VERSION = 0
    def command_editmeta(self,up,next,prev):
        self._up = up
        self._next = next
        self._prev = prev

    def content(self):
        meta = self.targetsForRelationship(MetaData.InformationOf)
        return meta[0].content()

    def allVersions(self):
        return self.metadataTargetsForRelationship(MetaData.InformationOf)

    def getXML(self):
        return """
<article class="activist.Article" id="%(id)s">
  <title>%(title)s</title>
  <content>%(content)s</content>
%(meta)s
</article>
        """ % { "content": self.content(),
                "id":self.id(),
                "title":self.title(),
                "meta":self.getMetaXML()
                }

    def __str__(self):
        return '<Article %s>' % self.id()
